﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;

namespace GpsLoggerConsole
{
    class Program
    {
        private const string COM_NAME = "COM6";
        private const int COM_BAUD = 19200;
        static SerialPort serialPort = new SerialPort();
        static string fileName;
        static StreamWriter sw;

        static void Main(string[] args)
        {
            
            serialPort.PortName = COM_NAME;
            serialPort.BaudRate = COM_BAUD;
            serialPort.StopBits = StopBits.One;
            serialPort.DataBits = 8;
            serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
            fileName = string.Format("{0:0000}{1:00}{2:00}{3:00}{4:00}{5:00}.txt", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            sw = new StreamWriter(fileName);
            serialPort.Open();

            Console.WriteLine("Press any key to stop...");
            Console.ReadKey();
            serialPort.Close();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
            sw.Close();

        }

        static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string indata = serialPort.ReadExisting();
            Console.WriteLine(indata);
            sw.WriteLine(indata);
        }

        public static void Read()
        {
            //File file = new File();
            while (true)
            {
                try
                {
                    string message = serialPort.ReadLine();
                    Console.WriteLine(message);
                    
                }
                catch (TimeoutException) { }
            }
        }
    }
}

/*
$GPGGA,,,,,,0,00,0.5,,M,,,,*00
$GPGGA,134536.00,4047.182345,N,04350.983003,E,1,05,0.5,1589.0,M,,,,*01
$GPRMC,134536.00,A,4047.182345,N,04350.983003,E,000.0,348.0,200913,,,A*53
$GPGSV,2,1,05,22,60,222,40,16,35,258,34,03,33,310,30,29,12,138,36*73

$GPGSV,2,2,05,14,04,174,41,,,,,,,,,,,,*4A
$GPGSA,A,3,03,14,16,22,29,,,,,,,,1.1,0.5,1.0*3C

$GPGGA,134539.00,4047.182345,N,04350.983003,E,1,05,0.5,1589.0,M,,,,*0E

$GPRMC,134539.00,A,4047.182345,N,04350.983003,E,000.0,348.0,200913,,,A*5C

$GPGSV,2,1,05,22,60,222,41,16,35,258,34,03,33,310,32,29,12,138,37*71

$GPGSV,2,2,05,14,04,174,40,,,,,,,,,,,,*4B

$GPGSA,A,3,03,14,16,22,29,,,,,,,,1.1,0.5,1.0*3C

$GPGGA,134542.00,4047.182260,N,04350.982997,E,1,05,0.5,1588.0,M,,,,*00

$GPRMC,134542.00,A,4047.182260,N,04350.982997,E,000.0,348.0,200913,,,A*53

$GPGSV,2,1,05,22,60,222,41,16,35,258,34,03,33,310,31,29,12,138,36*73

$GPGSV,2,2,05,14,04,174,42,,,,,,,,,,,,*49

$GPGSA,A,3,03,14,16,22,29,,,,,,,,1.1,0.5,1.0*3C


$GPRMC,202628.00,A,4047.187739,N,04350.988103,E,000.5,090.7,200913,,,A*79
$GPRMC,134536.00,A,4047.182345,N,04350.983003,E,000.0,348.0,200913,,,A*53
 * 
$GPGGA,004113.00,4047.192077,N,04350.990446,E,1,05,0.5,1574.0,M,,,,*22
$GPGGA,134536.00,4047.182345,N,04350.983003,E,1,05,0.5,1589.0,M,,,,*01
 * 
$GPRMC,202628.00,A,4047.187739,N,04350.988103,E,000.5,090.7,200913,,,A*79
$GPRMC,202629.00,A,4047.187739,N,04350.988103,E,000.5,090.7,200913,,,A*78
$GPRMC,202630.00,A,4047.187739,N,04350.988103,E,000.5,090.7,200913,,,A*70
$GPRMC,202631.00,A,4047.187739,N,04350.988103,E,000.5,090.7,200913,,,A*71
 * 
$GPGGA,,,,,,0,00,300.0,,M,,,,*06

$GPRMC,073512.00,V,,,,,,,210913,,,N*77

$GPGSV,1,1,03,24,47,196,35,29,23,252,36,05,02,142,32,,,,*4C

$GPGSA,A,1,05,24,29,,,,,,,,,,669.0,300.0,600.0*34

$GPGGA,,,,,,0,00,300.0,,M,,,,*06

$GPRMC,073515.00,V,,,,,,,210913,,,N*70

$GPGSV,1,1,03,24,47,196,35,29,23,252,37,05,02,142,32,,,,*4D

$GPGSA,A,1,05,24,29,,,,,,,,,,669.0,300.0,600.0*34

$GPGGA,,,,,,0,00,300.0,,M,,,,*06

$GPRMC,073518.00,V,,,,,,,210913,,,N*7D

$GPGSV,1,1,03,24,47,196,36,29,23,252,36,05,02,142,33,,,,*4E

$GPGSA,A,1,05,24,29,,,,,,,,,,669.0,300.0,600.0*34

$GPGGA,073521.00,4047.191225,N,04350.974617,E,1,03,5.0,1590.0,M,,,,*05

$GPRMC,073521.00,A,4047.191225,N,04350.974617,E,000.0,000.0,210913,,,A*57

$GPGSV,2,1,08,24,47,196,37,29,23,252,37,05,02,142,32,12,74,355,*74

$GPGSV,2,2,08,02,64,116,,04,39,057,,10,12,096,,14,10,296,*7F

$GPGSA,A,3,05,24,29,,,,,,,,,,11.2,5.0,10.0*3C

$GPGGA,073524.00,4047.191225,N,04350.974617,E,1,03,1.0,1590.0,M,,,,*04

$GPRMC,073524.00,A,4047.191225,N,04350.974617,E,000.0,000.0,210913,,,A*52

$GPGSV,2,1,08,24,47,196,39,29,23,252,36,05,02,142,27,12,74,355,*7F

$GPGSV,2,2,08,02,64,116,,04,39,057,,10,12,096,,14,10,296,*7F

$GPGSA,A,3,05,24,29,,,,,,,,,,2.2,1.0,2.0*39

$GPGGA,073527.00,4047.191225,N,04350.974617,E,1,03,1.0,1590.0,M,,,,*07

$GPRMC,073527.00,A,4047.191225,N,04350.974617,E,000.0,000.0,210913,,,A*51

$GPGSV,2,1,08,24,47,196,39,29,23,252,37,05,02,142,28,12,74,355,*71

$GPGSV,2,2,08,02,64,116,,04,39,057,,10,12,096,,14,10,296,*7F

$GPGSA,A,3,05,24,29,,,,,,,,,,2.2,1.0,2.0*39

$GPGGA,073530.00,4047.191225,N,04350.974617,E,1,03,1.0,1590.0,M,,,,*01

$GPRMC,073530.00,A,4047.191225,N,04350.974617,E,000.0,000.0,210913,,,A*57

$GPGSV,2,1,08,24,47,196,37,29,23,252,37,05,02,142,30,12,74,355,*76

$GPGSV,2,2,08,02,64,116,,04,39,057,,10,12,096,,14,10,296,*7F

$GPGSA,A,3,05,24,29,,,,,,,,,,2.2,1.0,2.0*39

$GPGGA,073533.00,4047.196990,N,04350.973562,E,1,03,1.0,1589.0,M,,,,*0E

$GPRMC,073533.00,A,4047.196990,N,04350.973562,E,000.0,000.0,210913,,,A*50

$GPGSV,2,1,08,24,47,196,37,29,23,252,35,05,02,142,28,12,74,355,*7D

$GPGSV,2,2,08,02,64,116,,04,39,057,,10,12,096,,14,10,296,*7F

$GPGSA,A,3,05,24,29,,,,,,,,,,2.2,1.0,2.0*39

$GPGGA,073536.00,4047.196990,N,04350.973562,E,1,03,1.0,1589.0,M,,,,*0B

$GPRMC,073536.00,A,4047.196990,N,04350.973562,E,000.0,000.0,210913,,,A*55

$GPGSV,2,1,08,24,47,196,37,29,23,252,35,05,02,142,28,12,74,355,*7D

$GPGSV,2,2,08,02,64,116,,04,39,057,,10,12,096,,14,10,296,*7F

$GPGSA,A,3,05,24,29,,,,,,,,,,2.2,1.0,2.0*39

$GPGGA,073539.00,4047.201859,N,04350.975383,E,1,03,0.5,1589.0,M,,,,*06

$GPRMC,073539.00,A,4047.201859,N,04350.975383,E,000.0,000.0,210913,,,A*5C

$GPGSV,2,1,08,24,47,196,40,29,23,252,35,05,02,142,30,12,74,355,*74

$GPGSV,2,2,08,02,64,116,,04,39,057,,10,12,096,,14,10,296,*7F

$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E

$GPGGA,073542.00,4047.201859,N,04350.975383,E,1,03,5.0,1589.0,M,,,,*0A

$GPRMC,073542.00,A,4047.201859,N,04350.975383,E,000.0,000.0,210913,,,A*50

$GPGSV,2,1,08,24,47,196,36,29,23,252,35,05,02,142,30,12,74,355,*75

$GPGSV,2,2,08,02,64,116,,04,39,057,,10,12,096,,14,10,296,*7F

$GPGSA,A,3,05,24,29,,,,,,,,,,11.2,5.0,10.0*3C

$GPGGA,073545.00,4047.207477,N,04350.977489,E,1,03,0.5,1591.0,M,,,,*0D

$GPRMC,073545.00,A,4047.207477,N,04350.977489,E,000.0,000.0,210913,,,A*5E

$GPGSV,2,1,08,24,47,196,33,29,23,252,35,05,02,142,28,12,74,355,*79

$GPGSV,2,2,08,02,64,116,,04,39,057,,10,12,096,,14,10,296,*7F

$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E

$GPGGA,073548.00,4047.207477,N,04350.977489,E,1,03,5.0,1591.0,M,,,,*00

$GPRMC,073548.00,A,4047.207477,N,04350.977489,E,000.0,000.0,210913,,,A*53

$GPGSV,2,1,08,24,47,196,34,29,23,252,35,05,02,142,29,12,74,355,28*75

$GPGSV,2,2,08,02,64,116,28,04,39,057,,10,12,096,,14,10,296,*75

$GPGSA,A,3,05,24,29,,,,,,,,,,11.2,5.0,10.0*3C

$GPGGA,073551.00,4047.207477,N,04350.977489,E,1,03,5.0,1591.0,M,,,,*08

$GPRMC,073551.00,A,4047.207477,N,04350.977489,E,000.0,000.0,210913,,,A*5B

$GPGSV,2,1,08,24,47,196,35,29,23,252,36,05,02,142,29,12,74,355,28*77

$GPGSV,2,2,08,02,64,116,28,04,39,057,,10,12,096,,14,10,296,*75

$GPGSA,A,3,05,24,29,,,,,,,,,,11.2,5.0,10.0*3C

$GPGGA,073554.00,4047.211013,N,04350.979240,E,1,03,0.5,1592.0,M,,,,*02

$GPRMC,073554.00,A,4047.211013,N,04350.979240,E,000.0,000.0,210913,,,A*52

$GPGSV,2,1,08,24,47,196,34,29,23,252,36,05,02,142,29,12,74,355,27*79

$GPGSV,2,2,08,02,64,116,31,04,39,057,,10,12,096,,14,10,296,*7D

$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E

$GPGGA,073557.00,4047.211013,N,04350.979240,E,1,03,0.5,1592.0,M,,,,*01

$GPRMC,073557.00,A,4047.211013,N,04350.979240,E,000.0,000.0,210913,,,A*51

$GPGSV,2,1,08,24,47,196,35,29,23,252,37,05,02,142,30,12,74,355,24*72

$GPGSV,2,2,08,02,64,116,31,04,39,057,,10,12,096,,14,10,296,*7D

$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E

$GPGGA,073600.00,4047.211013,N,04350.979240,E,1,03,0.5,1592.0,M,,,,*00

$GPRMC,073600.00,A,4047.211013,N,04350.979240,E,000.0,000.0,210913,,,A*50

$GPGSV,2,1,08,24,47,196,36,29,23,252,31,05,02,142,32,12,74,355,25*74

$GPGSV,2,2,08,02,64,116,32,04,39,057,,10,12,096,,14,10,296,*7E

$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E

$GPGGA,073603.00,4047.211013,N,04350.979240,E,1,03,0.5,1592.0,M,,,,*03

$GPRMC,073603.00,A,4047.211013,N,04350.979240,E,000.0,000.0,210913,,,A*53

$GPGSV,2,1,08,24,47,196,36,29,23,252,35,05,02,142,28,12,74,355,23*7D

$GPGSV,2,2,08,02,64,116,31,04,39,057,,10,12,096,,14,10,296,*7D

$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E

$GPGGA,073606.00,4047.208363,N,04350.977657,E,1,03,0.5,1591.0,M,,,,*05

$GPRMC,073606.00,A,4047.208363,N,04350.977657,E,000.0,000.0,210913,,,A*56

$GPGSV,2,1,08,24,47,196,37,29,23,252,35,05,02,142,28,12,74,355,21*7E

$GPGSV,2,2,08,02,64,116,31,04,39,057,,10,12,096,,14,10,296,*7D

$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E

$GPGGA,073609.00,4047.208363,N,04350.977657,E,1,03,0.5,1591.0,M,,,,*0A

$GPRMC,073609.00,A,4047.208363,N,04350.977657,E,000.0,000.0,210913,,,A*59

$GPGSV,2,1,08,24,47,196,37,29,23,252,36,05,02,142,31,12,74,355,23*77

$GPGSV,2,2,08,02,64,116,29,04,39,057,,10,12,096,,14,10,296,*74

$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E

$GPGGA,073612.00,4047.208363,N,04350.977657,E,1,03,0.5,1591.0,M,,,,*00

$GPRMC,073612.00,A,4047.208363,N,04350.977657,E,000.0,000.0,210913,,,A*53

$GPGSV,2,1,08,24,47,196,36,29,23,252,36,05,02,142,29,12,74,355,24*78

$GPGSV,2,2,08,02,64,116,31,04,39,057,,10,12,096,,14,10,296,*7D

$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E

$GPGGA,073615.00,4047.208363,N,04350.977657,E,1,03,0.5,1591.0,M,,,,*07

$GPRMC,073615.00,A,4047.208363,N,04350.977657,E,000.0,000.0,210913,,,A*54

$GPGSV,2,1,08,24,47,196,35,29,23,252,37,05,02,142,28,12,74,355,31*7F

$GPGSV,2,2,08,02,64,116,31,04,39,057,,10,12,096,,14,10,296,*7D

$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E

$GPGGA,073618.00,4047.208363,N,04350.977657,E,1,03,0.5,1591.0,M,,,,*0A

$GPRMC,073618.00,A,4047.208363,N,04350.977657,E,000.0,000.0,210913,,,A*59
$GPGSV,2,1,08,24,47,196,35,29,23,252,35,05,02,142,31,12,74,355,25*70
$GPGSV,2,2,08,02,64,116,31,04,39,057,,10,12,096,,14,10,296,*7D
$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E
$GPGGA,073621.00,4047.208363,N,04350.977657,E,1,03,5.0,1591.0,M,,,,*00
$GPRMC,073621.00,A,4047.208363,N,04350.977657,E,000.0,000.0,210913,,,A*53
$GPGSV,2,1,08,24,46,196,35,29,23,252,35,05,02,142,31,12,74,355,27*73
$GPGSV,2,2,08,02,64,116,33,04,39,057,,10,12,096,,14,10,296,*7F
$GPGSA,A,3,05,24,29,,,,,,,,,,11.2,5.0,10.0*3C
$GPGGA,073624.00,4047.208363,N,04350.977657,E,1,03,0.5,1591.0,M,,,,*05
$GPRMC,073624.00,A,4047.208363,N,04350.977657,E,000.0,000.0,210913,,,A*56
$GPGSV,2,1,08,24,46,196,33,29,23,252,34,05,02,142,34,12,74,355,24*72
$GPGSV,2,2,08,02,64,116,32,04,39,057,,10,12,096,,14,10,296,*7E
$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E
$GPGGA,073627.00,4047.205692,N,04350.975226,E,1,03,0.5,1593.0,M,,,,*02
$GPRMC,073627.00,A,4047.205692,N,04350.975226,E,000.0,000.0,210913,,,A*53
$GPGSV,2,1,08,24,46,196,34,29,23,252,34,05,02,142,34,12,74,355,22*73
$GPGSV,2,2,08,02,64,116,31,04,39,057,,10,12,096,,14,10,296,*7D
$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E
$GPGGA,073630.00,4047.205692,N,04350.975226,E,1,03,0.5,1593.0,M,,,,*04
$GPRMC,073630.00,A,4047.205692,N,04350.975226,E,000.0,000.0,210913,,,A*55
$GPGSV,2,1,08,24,46,196,36,29,23,252,35,05,02,142,32,12,74,355,18*7F
$GPGSV,2,2,08,02,64,116,26,04,39,057,,10,12,096,,14,10,296,*7B
$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E
$GPGGA,073633.00,4047.205692,N,04350.975226,E,1,03,5.0,1593.0,M,,,,*07
$GPRMC,073633.00,A,4047.205692,N,04350.975226,E,000.0,000.0,210913,,,A*56
$GPGSV,2,1,08,24,46,196,35,29,23,252,35,05,02,142,32,12,74,355,14*70
$GPGSV,2,2,08,02,64,116,28,04,39,057,,10,12,096,,14,10,296,*75
$GPGSA,A,3,05,24,29,,,,,,,,,,11.2,5.0,10.0*3C
$GPGGA,073636.00,4047.205692,N,04350.975226,E,1,03,0.5,1593.0,M,,,,*02
$GPRMC,073636.00,A,4047.205692,N,04350.975226,E,000.0,000.0,210913,,,A*53
$GPGSV,2,1,08,24,46,196,35,29,23,252,36,05,02,142,31,12,74,355,11*75
$GPGSV,2,2,08,02,64,116,26,04,39,057,,10,12,096,,14,10,296,*7B
$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E
$GPGGA,073639.00,4047.205692,N,04350.975226,E,1,03,5.0,1593.0,M,,,,*0D
$GPRMC,073639.00,A,4047.205692,N,04350.975226,E,000.0,000.0,210913,,,A*5C
$GPGSV,2,1,08,24,46,196,36,29,23,252,36,05,02,142,25,02,64,116,28*7C
$GPGSV,2,2,08,12,74,355,,04,39,057,,10,12,096,,14,10,296,*7A
$GPGSA,A,3,05,24,29,,,,,,,,,,11.2,5.0,10.0*3C
$GPGGA,073642.00,4047.205692,N,04350.975226,E,1,03,0.5,1593.0,M,,,,*01
$GPRMC,073642.00,A,4047.205692,N,04350.975226,E,000.0,000.0,210913,,,A*50
$GPGSV,2,1,08,24,46,196,36,29,23,252,36,05,02,142,28,12,74,355,24*78
$GPGSV,2,2,08,02,64,116,30,04,39,057,,10,12,096,,14,10,296,*7C
$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E

$GPGGA,073645.00,4047.205692,N,04350.975226,E,1,03,5.0,1593.0,M,,,,*06

$GPRMC,073645.00,A,4047.205692,N,04350.975226,E,000.0,000.0,210913,,,A*57

$GPGSV,2,1,08,24,46,196,34,29,23,252,35,05,02,142,27,12,74,355,24*76

$GPGSV,2,2,08,02,64,116,26,04,39,057,,10,12,096,,14,10,296,*7B

$GPGSA,A,3,05,24,29,,,,,,,,,,11.2,5.0,10.0*3C

$GPGGA,073648.00,4047.205692,N,04350.975226,E,1,03,5.0,1593.0,M,,,,*0B
$GPRMC,073648.00,A,4047.205692,N,04350.975226,E,000.0,000.0,210913,,,A*5A

$GPGSV,2,1,08,24,46,196,35,29,23,252,36,05,02,142,30,12,74,355,25*73
$GPGSV,2,2,08,02,64,116,27,04,39,057,,10,12,096,,14,10,296,*7A

$GPGSA,A,3,05,24,29,,,,,,,,,,11.2,5.0,10.0*3C

$GPGGA,073651.00,4047.205692,N,04350.975226,E,1,03,0.5,1593.0,M,,,,*03

$GPRMC,073651.00,A,4047.205692,N,04350.975226,E,000.0,000.0,210913,,,A*52

$GPGSV,2,1,08,24,46,196,35,29,23,252,37,05,02,142,32,12,74,355,24*71

$GPGSV,2,2,08,02,64,116,25,04,39,057,,10,12,096,,14,10,296,*78

$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E

$GPGGA,131527.00,4047.272   ,N,04350.364   ,E,1,05,0.5,0.0,M,,,,*13
$GPGGA,073654.00,4047.205692,N,04350.975226,E,1,03,0.5,1593.0,M,,,,*06

$GPRMC,132016.00,A,4047.272546,N,04350.364495,E,000.0,000.0,210913,,,A*1C
$GPRMC,073654.00,A,4047.205692,N,04350.975226,E,000.0,000.0,210913,,,A*57

$GPGSV,2,1,08,24,46,196,34,29,23,252,36,05,02,142,35,12,74,355,24*76

$GPGSV,2,2,08,02,64,116,27,04,39,057,,10,12,096,,14,10,296,*7A

$GPGSA,A,3,05,24,29,,,,,,,,,,1.1,0.5,1.0*3E


 */
